package com.knowledge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knowledge.common.constant.GlobalConstant;
import com.knowledge.common.enums.NewsTypeEnum;
import com.knowledge.dao.entity.FriendApplyEntity;
import com.knowledge.dao.entity.NewsEntity;
import com.knowledge.dao.entity.SysUser;
import com.knowledge.dao.mapper.FriendApplyMapper;
import com.knowledge.service.FriendApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowledge.service.FriendService;
import com.knowledge.service.NewsService;
import com.knowledge.service.SysUserService;
import com.knowledge.utils.BeanMapper;
import com.knowledge.utils.ListUtil;
import com.knowledge.utils.TemplateParserUtil;
import com.knowledge.vo.FriendApplyVO;
import com.knowledge.vo.Result;
import com.knowledge.vo.params.FriendParam;
import com.knowledge.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 申请添加好友表 服务实现类
 * </p>
 *
 * @author zzz
 * @since 2022-03-03
 */
@Service
public class FriendApplyServiceImpl extends ServiceImpl<FriendApplyMapper, FriendApplyEntity> implements FriendApplyService {

    @Autowired
    private NewsService newsService;

    @Autowired
    private FriendService friendService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public Result listFriendApplyWithPage(Long coverUserId, PageParams pageParams) {
        if (pageParams == null){
            pageParams = new PageParams();
        }
        Page<FriendApplyEntity> page = new Page<>(pageParams.getPage(), pageParams.getPageSize());
        QueryWrapper<FriendApplyEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("cover_user_id", coverUserId);
        queryWrapper.eq("is_agree", GlobalConstant.NO);
        queryWrapper.orderByDesc("create_time");
        page = page(page, queryWrapper);
        List<FriendApplyVO> friendApplyList = BeanMapper.mapList(page.getRecords(), FriendApplyEntity.class, FriendApplyVO.class);
        //关联用户信息
        List<Long> applyUserIds = friendApplyList.stream().map(FriendApplyVO::getApplyUserId).collect(Collectors.toList());
        if (applyUserIds.size() > 0){
            List<SysUser> userList = sysUserService.listByIds(applyUserIds);
            ListUtil.setListByEqualObjProperty(friendApplyList, "applyUserId", "applyUserName",
                    userList, "id", "nickname");
        }
        return Result.success(friendApplyList);
    }

    @Override
    public void applyFriend(FriendParam friendParam) {
        //查询是否申请了
        QueryWrapper<FriendApplyEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("apply_user_id", friendParam.getUserId());
        queryWrapper.eq("cover_user_id", friendParam.getFriendUserId());
        queryWrapper.eq("is_agree", GlobalConstant.NO);
        FriendApplyEntity friendApplyEntity = getOne(queryWrapper);
        if (friendApplyEntity == null){
            friendApplyEntity = new FriendApplyEntity();
            friendApplyEntity.setApplyUserId(friendParam.getUserId());
            friendApplyEntity.setCoverUserId(friendParam.getFriendUserId());
            friendApplyEntity.setIsAgree(GlobalConstant.NO);
            friendApplyEntity.setCreateTime(new Date());
            friendApplyEntity.setUpdateTime(new Date());
            //好友申请记录
            this.save(friendApplyEntity);

            //好友申请消息
            NewsEntity newsEntity = new NewsEntity();
            newsEntity.setUserId(friendApplyEntity.getCoverUserId());
            newsEntity.setFromUserId(friendApplyEntity.getApplyUserId());
            newsEntity.setType(NewsTypeEnum.APPLY_FRIEND.getType());
            String contentTemplate = NewsTypeEnum.APPLY_FRIEND.getContentTemplate();
            Map<String, String> contentMap = new HashMap<>();
            contentMap.put("userName", friendParam.getUserName());
            newsEntity.setContent(TemplateParserUtil.parse(contentTemplate, contentMap));
            newsService.addNews(newsEntity);
        }
    }

    @Override
    public void agreeFriendApply(Integer applyId) {
        FriendApplyEntity friendApplyEntity = getById(applyId);
        //修改同意
        UpdateWrapper<FriendApplyEntity> updateWrapper = new UpdateWrapper();
        updateWrapper.lambda().set(FriendApplyEntity::getIsAgree, GlobalConstant.YES)
                .eq(FriendApplyEntity::getId, applyId);
        update(updateWrapper);

        //添加好友关系
        friendService.addFriend(friendApplyEntity.getApplyUserId(), friendApplyEntity.getCoverUserId());

        //发送同意消息
        NewsEntity newsEntity = new NewsEntity();
        newsEntity.setUserId(friendApplyEntity.getApplyUserId());
        newsEntity.setFromUserId(friendApplyEntity.getCoverUserId());
        newsEntity.setType(NewsTypeEnum.AGREE_FRIEND_APPLY.getType());
        String contentTemplate = NewsTypeEnum.AGREE_FRIEND_APPLY.getContentTemplate();
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("userName", sysUserService.findUserById(newsEntity.getFromUserId()).getNickname());
        newsEntity.setContent(TemplateParserUtil.parse(contentTemplate, contentMap));
        newsService.addNews(newsEntity);
    }
}
